<template>
    <div>
        <van-popup @close="close" v-model="show" position="bottom" :style="{ 'height': popupHeight }">
            <div class="date-popup-box" v-if="!(dateS.start || dateS.end)">
                <div class="date-row">开始时间:<div class="date-box" @click="dateS.start = true">{{ date_str_format2.start
                        }}
                    </div>
                </div>
                <div class="date-row">结束时间:<div class="date-box" @click="dateS.end = true">{{ date_str_format2.end }}
                    </div>
                </div>
            </div>
            <div class="date-picker" v-if="dateS.start || dateS.end">
                <h3 class="date-select-title">{{ dateS.start ? '开始' : '结束' }}</h3>
                <div class="datetime-picker">
                    <div class="date-text-row">
                        <span>年</span>
                        <span>月</span>
                        <span>日</span>
                        <span>时</span>
                        <span>分</span>
                    </div>
                    <div class="datetime-picker-a">
                        <van-datetime-picker :max-date="date.end" v-if="dateS.start" :show-toolbar="false"
                            v-model="date.start" type="datehours" title="选择年月日小时">
                        </van-datetime-picker>
                        <van-datetime-picker :min-date="date.start" :max-date="today" v-else :show-toolbar="false"
                            v-model="date.end" type="datehours" title="选择年月日小时">
                        </van-datetime-picker>
                    </div>
                </div>
                <div class="btn-row">
                    <van-button type="info" @click="cancel_date()">确认</van-button>
                </div>
            </div>
            <div v-else>
                <div class="group-2">


                    <van-button type="info" @click="setDate(item.val)" :key="'s-' + index"
                        v-for="(item, index) in dateList">{{
                        item.label }}</van-button>
                </div>
                <div class="sbumit-row-2">
                    <van-button type="info" @click="verify_date()">确认</van-button>
                    <van-button type="default" @click="close()">取消</van-button>
                </div>
            </div>
        </van-popup>
    </div>
</template>
<script>

    function format_date(dateObj) {
        return dateObj.getFullYear() + "年" + (dateObj.getMonth() + 1) + "月" + (dateObj.getDate()) + "日"+dateObj.getHours()+"时"+dateObj.getMinutes()+"分";
    }
    export default {
        props: ['popup_show', 'vdate'],
        data() {
            var now_date = new Date();
            var pass_date = new Date();
            pass_date.setDate(now_date.getDate() - 5);
            var date = { "start": pass_date, "end": now_date };
            var dateList = [
                { "label": "近1天", "val": 1 },
                { "label": "近3天", "val": 3 },
                { "label": "近7天", "val": 7 }
            ];//用于快速选择
            return {
                dateS: { "start": false, "end": false },
                date,
                show: false,
                today: now_date,
                dateList
            }
        },
        watch: {
            popup_show(new_val, old_val) {
                if (new_val !== old_val) {
                    this.dateS = {
                        start: false,
                        end: false
                    }
                    this.show = new_val;
                    this.date.start = this.vdate[0];
                    this.date.end = this.vdate[1];
                }
            },
            dateS: {
                handler(new_val, old_val) {
                    if (new_val.start !== old_val.start) {
                        if (new_val.start) {
                            this.date.start = this.vdate[0]
                        }
                    }
                    if (new_val.end !== old_val.end) {
                        if (new_val.end) {
                            this.date.end = this.vdate[1]
                        }
                    }
                },
                deep: true
            }
        },
        computed: {
            popupHeight() {
                return (this.dateS.start || this.dateS.end) ? '60%' : '60%'
            },
            date_str_format2() {
                var start = format_date(this.date.start);
                var end = format_date(this.date.end);
                return {
                    start, end
                }
            },
        },
        methods: {
            setDate(val) {
                var now_date = new Date();
                var pass_date = new Date();
                pass_date.setDate(now_date.getDate() - val);
                this.date = { "start": pass_date, "end": now_date };
            },
            cancel_date() {
                this.dateS.end = false;
                this.dateS.start = false;
            },
            verify_date() {
                this.$emit("dateRes", this.date);
                this.close();
            },
            close() {
                this.dateS.start = false;
                this.dateS.end = false;
                this.$emit("close", false);
            }
        }
    }
</script>
<style scoped>
    h3.date-select-title {
        text-align: center;
        padding-bottom: 30px;
    }

    .btn-row {
        display: flex;
        justify-content: space-around;
        margin-top: 30px;
        padding-bottom: 30px;
    }


    .date-box {
        flex: 1;
        border: 1px solid lightgrey;
        padding: 10px;
        border-radius: 5px;
        margin-left: 20px;
        /* margin-top: 20px; */
    }

    .date-popup-box {
        margin: 10px;
    }

    .btn-row {
        display: flex;
        justify-content: space-around;
        margin-top: 30px;
        padding-bottom: 30px;
    }

    .date-text-row {
        display: flex;
        justify-content: space-around;
    }

    .sbumit-row-2 {
        display: flex;
        justify-content: space-evenly;
        /* padding: 25px 0; */
        padding-top: 30px;
    }

    .date-row {
        display: flex;
        padding: 10px 20px;
        align-items: center;
        margin-top: 20px;
        border-bottom: 1px solid #e5e5e5;
    }

    .group-2 {
        display: flex;
        justify-content: space-around;
        flex-wrap: wrap;
        padding: 25px;
    }

    .group-2 button {
        margin: 15px;
    }
</style>